import LGXCOM from '../common/common.js'
import baselinegraph from './linegraph.js'
import { CoordTRFUtil } from '../common/tools/coordtool.js'
import lnActPkg from './action/auxlineaction.js'
import UtilFuncs from '../common/tools/utilfuncs.js'
var funcs = UtilFuncs.UtilFuncs;

import globalConst from '../common/globalConst.js'
var global = globalConst.GlobalConst


import {LGXEnums}  from '../common/sysenum.js'

import arrowutil from './paintutil/arrowutil.js'

class AuxLineGraph extends baselinegraph.LineGraph {
    constructor(type) {
        super(type);
        this.m_graphType = LGXEnums.LGXGraphType.Auxline_type;
        var action = new lnActPkg.AuxLineGraphAction(this);
        this.setActionSvc(action);
        this.raylineFlag = false;//是否单向射线
        this.rayLineVector = new LGXCOM.LGXVector();
        this.stPoint = new LGXCOM.LGXPoint();
        this._lineDef.color.setVal(0,255,0);
    }

    setStPoint(pt) {
        if (!pt.x || !pt.y){
            console.log("erorr data");
        }
        this.stPoint.clone(pt);
    }

    setSecondPoint(pt) {
        let dis = funcs.distancePoint(this.stPoint, pt,1);
        if (dis < global.ZERO_4E){
            this.rayLineVector.setValue(1,0,0);
        }
        else{
            this.rayLineVector.setBy2Pt(this.stPoint, pt);
        }
    }
    setRaylineFlag(v){this.raylineFlag = v;}
    getRaylineFlag(){this.raylineFlag;}

    isDoubleRayLine(){return !this.raylineFlag;}
    getRayStPt(){return this.stPoint;}
    getRayLineVector(){return this.rayLineVector;}
    setRayLineVector(v){this.rayLineVector.clone(v);}

    display(map, mapInfo,forClip) {
        // console.log("aux line display");
        // console.log(mapInfo.viewPortRect);
        var position = this.getPos();
        var ptCount = position.posList.length;
        if (ptCount < 2) {
            this.setUseful(0);
            return;
        }
        map.save();
        map.beginPath();
        map.lineWidth = 1;
        map.strokeStyle = this._lineDef.color.toHexString();
        map.setLineDash([3,0,3]);


        let positiveVector = this.rayLineVector;
        let negtiveVector = this.rayLineVector.rotateCopy(180);

        let ptlst = [];

        let interViewRectPt1 = this.getRayInterViewPornt(this.stPoint, positiveVector, mapInfo.viewPortRect);
        let p = CoordTRFUtil.world2Screen(interViewRectPt1, mapInfo);
        map.moveTo(p.x, p.y);
        ptlst.push(p);

        //起点不需要显示箭头，那就是射线
        let interViewRectPt2 = null;
        if (!this.raylineFlag) {
            interViewRectPt2 = this.getRayInterViewPornt(this.stPoint, negtiveVector, mapInfo.viewPortRect);
            p = CoordTRFUtil.world2Screen(interViewRectPt2, mapInfo);
            map.lineTo(p.x, p.y);
            ptlst.push(p);
        }
        else {
            p = CoordTRFUtil.world2Screen(this.stPoint, mapInfo);
            map.lineTo(p.x, p.y);
            ptlst.push(p);
        }

        map.stroke();
        map.closePath();

        let util = arrowutil.ArrowUtil;
        util.dis(interViewRectPt1, negtiveVector, map, mapInfo, this._lineDef.color.toHexString(), false, false);

        if (!this.raylineFlag) {
            util.dis(interViewRectPt2, positiveVector, map, mapInfo, this._lineDef.color.toHexString(), false, false);
        }

        super.displayGraphScope(map, mapInfo);
        this.displayFeaturePoint(map, ptlst, mapInfo);
        map.restore();
    }


    distance(tPoint, scaling, mapscale) {
        var position = this.getPos();
        var lenth = position.posList.length;
        if (lenth != 2) {
            return -1;
        }
        var cmpGap = super.getSelCmpGap(scaling, mapscale);
        var pPrePos = position.posList[0];
        let pCntPos = position.posList[1];
        let rst = funcs.distanceRayLine(tPoint, pPrePos, pCntPos, scaling);
        if (rst.dis > cmpGap) {
            return -1;
        }
        else if (!this.raylineFlag){
            return rst.dis;
        }
        else{
            let mouseProj2RayLinePt = funcs.getPointProj2Line(tPoint,this.stPoint,this.rayLineVector);
            let vct = funcs.getVector(this.stPoint,mouseProj2RayLinePt);
            let angle = this.rayLineVector.angleByCrossMultiply2Another(vct);
            if (Math.abs(angle) < global.ZERO_4E){
                return rst.dis;
            }
        }
        return -1;
    }

    calculateSelKind(tPoint, scaling, mapscale) {
        var position = this.getPos();
        var lenth = position.posList.length;
        if (lenth != 2) {
            return 0;
        }
        let mouseProj2RayLinePt = funcs.getPointProj2Line(tPoint,this.stPoint,this.rayLineVector);
        var cmpGap = super.getSelCmpGap(scaling, mapscale);
        var pPrePos = position.posList[0];
        let pCntPos = position.posList[1];
        let rst = funcs.distanceRayLine(tPoint, pPrePos, pCntPos, scaling);
        if (rst.dis > cmpGap) {
            return 0;
        }
        else if (!this.raylineFlag){
            tPoint.clone(mouseProj2RayLinePt);
            return 1;
        }
        else{
            let vct = funcs.getVector(this.stPoint,mouseProj2RayLinePt);
            let angle = this.rayLineVector.angleByCrossMultiply2Another(vct);
            if (Math.abs(angle) < global.ZERO_4E){
                tPoint.clone(mouseProj2RayLinePt);
                return 1;
            }
        }
        return 0;
    }

    movePosOffset(tOffset, modCmdInfo) {
        if (this._selKind < 1) {
            return;
        }

        var position = this.getPos();
        var lenth = position.posList.length;
        for (var i = 0; i < lenth; i++) {
            var pPos = position.posList[i];
            pPos.x += tOffset.x;
            pPos.y += tOffset.y;
        }
        this.stPoint.x += tOffset.x;
        this.stPoint.y += tOffset.y;
    }

    getRayInterViewPornt(pt, vector, viewPortRect) {
        let viewLTPt = viewPortRect.ltPt();
        let viewLBPt = viewPortRect.lbPt();
        let viewRTPt = viewPortRect.rtPt();
        let viewRBPt = viewPortRect.rbPt();

        let list = [];
        let viewVector = funcs.getVector(viewLTPt, viewLBPt);
        let p1 = funcs.getTwoLineInterPos(pt, vector, viewLTPt, viewVector);
        if (p1) {
            list.push(p1);
        }

        viewVector = funcs.getVector(viewLTPt, viewRTPt);
        let p2 = funcs.getTwoLineInterPos(pt, vector, viewLTPt, viewVector);
        if (p2) {
            list.push(p2);
        }

        viewVector = funcs.getVector(viewRBPt, viewRTPt);
        let p3 = funcs.getTwoLineInterPos(pt, vector, viewRBPt, viewVector);
        if (p3) {
            list.push(p3);
        }

        viewVector = funcs.getVector(viewRBPt, viewLBPt);
        let p4 = funcs.getTwoLineInterPos(pt, vector, viewRBPt, viewVector);
        if (p4) {
            list.push(p4);
        }

        let interInfoLst = [];
        for (let i = 0; i < list.length; i++) {
            let p = list[i];
            let vct = funcs.getVector(pt, p);
            let dis = funcs.distancePoint(pt, p);
            let angle = vct.angleByCrossMultiply2Another(vector);
            if (angle < global.ZERO_4E) {
                let t = { dis, pt: p };
                interInfoLst.push(t);
            }
        }

        //距离排序
        let sortedInterPointInfoList = interInfoLst.sort(function (p1, p2) {
            if ((p1.dis - p2.dis) > 0) {
                return 1;
            }
            else {
                return -1;
            }
        }
        );

        let dstPt = pt;
        if (sortedInterPointInfoList.length > 0) {
            dstPt = sortedInterPointInfoList[0].pt;
        }
        return dstPt;
    }

    assignInfo(info) {
        super.assignInfo(info);
        let position = this.getPosCopy();
        let p1 = position.posList[0];
        let p2 = position.posList[1];

        if (isNaN(p1.x) || isNaN(p1.y) || isNaN(p2.x) || isNaN(p2.y)) {
            if (this.getFinish() == 1){
                this.setUseful(0);
                return ;
            }
        }

        this.setStPoint(p1);
        this.setSecondPoint(p2);
    }

  
    toInfo() {
         let info = super.toInfo();
        return info;
    }

    //复制粘贴时，从复制源对象复制属性到本图形对应属性----虚函数，由实体继承类实现
    copySubPropFromBuf(bufInfo,tx,ty){
        this.copyCommnonPropFromBuf(bufInfo,tx,ty);
    }
}

export default { AuxLineGraph }